#/usr/bin/env python
# -*- coding: utf-8 -*-

'''
Created on 2011-11-09

@author: zhongfeng
'''

import re
from pageparser import *

egouRoot = ObuyUrlSummary(url=r'http://www.egou.com/', name='egou')
mainHost = 'http://www.egou.com'

def filterCatName(name):
    p = re.compile(r'\([0-9]*\)')
    return p.sub('',name)

class EGouSortParser(RootCatagoryPageParser):
    '''
            从http://www.egou.com/获取所有的分类信息
    '''   
    def __init__(self, dataStr, rootUrlSummary, include=None, exclude=None):
        super(EGouSortParser, self).__init__(dataStr, rootUrlSummary, include, exclude) 
                             
    def _getBaseSort1UrlSums(self):       
        finalUrlList = []
        allSort = self.soup.find(name='div',attrs={'id':'_JD_ALLSORT'})
        for t in allSort.findAll(name='div',attrs={'class':'item'}):#一级分类
            name,url = ParserUtils.parserTag_A(t.span.a)
            name = filterCatName(name)
            url = ''.join((mainHost,url))
            sort_1_urlsum = self.buildSort_N(url, name, self.rootUrlSummary, isCrawle=False)
            #sort_2 = t.find(attrs={'class':'subitem'})
            #for tt in sort_2(name='dt'):#二级分类
            #    name, url = ParserUtils.parserTag_A(tt.a)
            #    sort_2_urlsum = self.buildSort_N(url, name, sort_1_urlsum, isCrawle=True)
            #    finalUrlList.append(sort_2_urlsum)
            finalUrlList.append(sort_1_urlsum)
        return finalUrlList
    
    def parserSubUrlSums(self):
        result = self._getBaseSort1UrlSums() 
        return self.filterUrlList(result) 
        
class EGouSort1PageParser(RootCatagoryPageParser):
    '''
         一级页面解析类
    '''    
    def __init__(self, dataStr, rootUrlSummary, include=None, exclude=None):
        super(EGouSort1PageParser, self).__init__(dataStr, rootUrlSummary, include, exclude) 
                             
    def getNextSortUrlSums(self,firstFinalPage = False):       
        finalUrlList = []
        rSort2 = self.soup.find(name='div',attrs={'class':'bi_mainBox_L_1_m_2_1','style':'padding-left:18px;'})
        for t in rSort2.findAll(name='div',attrs={'class':'cat_1'}):
            name,url = ParserUtils.parserTag_A(t.a)
            name = filterCatName(name)
            url = ''.join((mainHost,url))
            sort_2_urlsum = self.buildSort_N(url, name, self.rootUrlSummary,firstFinalPage = firstFinalPage)
            finalUrlList.append(sort_2_urlsum)
            
        rSort2_more = self.soup.find(name='div',attrs={'id':'biPopLayer2'})
        if rSort2_more:
            for t in rSort2_more(name='a'):
                name,url = ParserUtils.parserTag_A(t)
                name = filterCatName(name)
                url = ''.join((mainHost,url))
                sort_2_urlsum = self.buildSort_N(url, name, self.rootUrlSummary,firstFinalPage = firstFinalPage)
                finalUrlList.append(sort_2_urlsum)
        return finalUrlList
    
    def parserSubUrlSums(self):
        result = self.getNextSortUrlSums() 
        return self.filterUrlList(result) 
    
class EGouSort2PageParser(EGouSort1PageParser):
    '''
          二级页面解析类
    '''    
    def __init__(self, dataStr, rootUrlSummary, include=None, exclude=None):
        super(EGouSort2PageParser, self).__init__(dataStr, rootUrlSummary, include, exclude) 
    
    def parserSubUrlSums(self):
        result = self.getNextSortUrlSums(firstFinalPage = True) 
        return self.filterUrlList(result) 

parserDict = {0:EGouSortParser, 1:EGouSort1PageParser,2:EGouSort2PageParser}
            
''' test '''
import os
curModDir = os.path.dirname(os.path.abspath(__file__))
testFilePath = os.path.join(curModDir,'test_resources')
def testEgouSortPage():
    fileName = os.path.join(testFilePath,'egouAllSort.html')
    with open(fileName, 'r') as fInput:
        content = fInput.read()
    telCat = ObuyUrlSummary(url='http://www.egou.com/browse07.01/',catagoryLevel=1,parentPath=[egouRoot])
    firstPage = EGouSortParser(content, egouRoot,include=(telCat,))
    for sort_1 in firstPage.parserSubUrlSums():       
        for index, urlsum in enumerate(sort_1.parentPath):
            pass
        print sort_1.name,sort_1.url ,sort_1.catagoryLevel

def testEgouSort1Page():
    fileName = os.path.join(testFilePath,'browse08.3085_sort1.html')
    with open(fileName, 'r') as fInput:
        content = fInput.read()
    egSortUrl = ObuyUrlSummary(url='http://www.egou.com/browse08.3085',catagoryLevel=1)
    firstPage = EGouSort1PageParser(content, egouRoot)
    for sort_2 in firstPage.parserSubUrlSums():
        print sort_2.name,sort_2.url ,sort_2.catagoryLevel

def testEgouSort2Page():
    fileName = os.path.join(testFilePath,'browse07.01.01.html')
    with open(fileName, 'r') as fInput:
        content = fInput.read()
    egSortUrl = ObuyUrlSummary(url='http://www.egou.com/browse07.01.01',catagoryLevel=3)
    firstPage = EGouSort2PageParser(content, egSortUrl)
    for sort_2 in firstPage.parserSubUrlSums():
        print sort_2.name,sort_2.url ,sort_2.catagoryLevel

if __name__ == '__main__':
    #testRegx()
    #testDangDangAllSortPage()
    #testSort3Page()
    testEgouSort2Page()
    #testEgouSort1Page()
   
    

    
    
